﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.JoinParty.BLL;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Core.Util;

namespace JoinParty.admin.FlowStep
{
    public partial class JoinPartyInspectionTEdit : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            var example = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (example != null)
            {
                PartyJoinApplycation app = ProcesUtil.GetApplycation(example.AppllicationID);
                this.hdAppID.Value = app.AppllicationID;
                this.hdAppLoginID.Value = app.LoginID;
                this.hdCurrentStandID.Value = this.CurrentStandID;
                this.hdUserName.Value = app.UserName;
                if (base.ReadOnly == "1")
                {
                    this.btnSaveClose.Hidden = true;
                }
            }

        }
        private void LoadData(string IsSave = "")
        {
            this.txtTitle.Text = DateTime.Now.ToString("yyyy-MM-dd") + "思想汇报";
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(this.ID))
            {
                var rep = DB.PartyJoinThoughtReports.Find(this.ID);
                if (rep != null)
                {
                    this.txtName.Text = this.hdUserName.Value;
                    this.txtTitle.Text = rep.Title;
                    this.ckContent.Text = rep.Content;
                    this.dtReportTime.Text = Convert.ToDateTime(rep.ReportTime).ToString("yyyy-MM-dd");
                }
            }
            else
            {
                this.dtReportTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtName.Text = this.hdUserName.Value;
            }

        }



        private void SaveData(bool isClose)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过

            bool bResult = false;
            var rep = new PartyJoinThoughtReport();
            if (!string.IsNullOrEmpty(this.ID))
            {
                rep = DB.PartyJoinThoughtReports.Find(this.ID);
                if (rep != null)
                {
                    rep.LoginID = this.hdAppLoginID.Value;
                    rep.Title = txtTitle.Text;

                    rep.ReportTime = Convert.ToDateTime(this.dtReportTime.Text);
                    rep.Content = ckContent.Text;
                    rep.StandID = this.CurrentStandID;
                    rep.CreateTime = DateTime.Now;
                    bResult = DB.SaveChanges() > 0;
                }
            }
            else
            {
                rep.LoginID = this.hdAppLoginID.Value;
                rep.Title = txtTitle.Text;

                rep.ReportTime = Convert.ToDateTime(this.dtReportTime.Text);
                rep.Content = ckContent.Text;
                rep.StandID = this.CurrentStandID;
                rep.CreateTime = DateTime.Now;
                rep.ID = StringHelper.NewID;
                DB.PartyJoinThoughtReports.Add(rep);
                bResult = DB.SaveChanges() > 0;
            }
        }


        #endregion

        #region
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(FilterSpecialCharacters.Filter(this.ckContent.Text.Trim())))
            {
                Alert.Show("汇报内容不能为空，请确认！", String.Empty, ActiveWindow.GetHideReference());
                return;
            };
            this.btnSaveClose.Visible = false;
            SaveData(true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}